﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MSIACCSL2.ViewModels;
using MSIACCSL2.Views.FormSearch;
using WcfService1.DTO;

namespace MSIACCSL2.Views.PurchaseInvoice
{
    public partial class FrmAdd : Page
    {
        public PurchaseInvoiceWrapViewModel WrapViewModel
        {
            get
            {
                return this.Resources["POW"] as PurchaseInvoiceWrapViewModel;
            }
        }

        public PurchaseInvoiceViewModel ViewModel
        {
            get
            {
                return this.DataContext as PurchaseInvoiceViewModel;
            }
        }

        public FrmAdd()
        {
            InitializeComponent();

            this.Loaded += (x,y) =>
                               {
                                   (this.Resources["POW"] as PurchaseInvoiceWrapViewModel).PurchaseInvoiceViewModel =
                                        (PurchaseInvoiceViewModel)this.DataContext;
                                   txtDate.Focus();
                               };
          

            this.txtSupplier.KeyDown += (o, e) =>
            {
                if (e.Key == Key.Enter)
                {
                    SupplierSearch supplierSearch = new SupplierSearch();
                    supplierSearch.Closed += new EventHandler(supplierSearch_Closed);
                    supplierSearch.Show();
                    supplierSearch.txtSupplierName.Text = txtSupplier.Text;
                    supplierSearch.txtSupplierName.Focus();
                }
            };


            this.KeyDown += (o, e) =>
            {
           
                if (e.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
                    ViewModel.SavePurchaseInvoice(ViewModel.PurchaseInvoice);
                else if (e.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
                    ViewModel.Cancel(null);
            };
        }

        void supplierSearch_Closed(object sender, EventArgs e)
        {
            ViewModel.PurchaseInvoice.Supplier = (sender as SupplierSearch).Supplier;
            txtSupplier.Focus();
        }
      

        private void txtPrice_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	(sender as TextBox).SelectAll();
        }

        private void textBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	(sender as TextBox).SelectAll();
        }


        private void txtNoLog_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

        private void txtNoLog_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                WrapViewModel.GetInventory(((TextBox)sender).DataContext);
        }

        //Lookup Inventory
        private void btnfind_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            InventorySearch inventorySearch = new InventorySearch();
            inventorySearch.Closed += new EventHandler(inventorySearch_Closed);
            inventorySearch.Show();
        }


        //Search Inventory Finished
        void inventorySearch_Closed(object sender, EventArgs e)
        {
            var inv = (sender as InventorySearch).Inventory;
            if (inv == null) return;
            var piDetail = dg1.SelectedItem as PurchaseInvoiceDetailDTO;
            piDetail.Inventory = inv;
            dg1.Focus();
        }

        private void checkBox_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if ((sender as CheckBox).IsChecked ?? false)
        	    ViewModel.LoadBTB((sender as CheckBox).Content);
            else
                ViewModel.UnloadBTB((sender as CheckBox).Content);
        }

        private void btnCloseStatus_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ViewModel.CloseStatus();
        }

        private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            //PurchaseInvoiceDetailDTO piDetail = (sender as TextBox).DataContext as PurchaseInvoiceDetailDTO;
            //if (piDetail != null)
            //    if (piDetail.Inventory.Item.IsRounded)
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:N}", piDetail.Inventory.QtyUnit);
            //    }
            //    else
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:0.0000}", piDetail.Inventory.QtyUnit);
            //    }
        }
     

    }
}

